﻿using System;
using System.Windows.Forms;
using Data.ViewModel;
using DataAccess.BikeManage;
using WinForm.Helper;

namespace WinForm.Dialog
{
    public partial class EditDispatch : Form
    {
        public EditDispatch()
        {
            InitializeComponent();
        }

        public EditDispatch(DispatchView from, DispatchView to)
        {
            InitializeComponent();

            txtFromAddress.Text = from.Address;
            txtCount1.Text = from.Count.ToString();
            lblFromID.Text = from.ID.ToString();
            txtToAddress.Text = to.Address;
            txtCount2.Text = to.Count.ToString();
            lblToID.Text = to.ID.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtDispatchCount.Text) || string.IsNullOrWhiteSpace(txtDispatchCount.Text))
            {
                lblTips.Visible = true;
            }
            else
            {
                try
                {
                    if (Convert.ToInt32(txtDispatchCount.Text) <= Convert.ToInt32(txtCount1.Text))
                    {
                        int plusid = Convert.ToInt32(lblToID.Text);
                        int subid = Convert.ToInt32(lblFromID.Text);
                        int count = Convert.ToInt32(txtDispatchCount.Text);
                        var db = new Dispatch();
                        bool result = Convert.ToBoolean(db.DoDispatch(plusid, subid, count));
                        if (result)
                        {
                            MyMessageBox.ShowMessageBoxInfo("调度成功！");
                            DialogResult = DialogResult.OK;
                        }
                        else
                        {
                            MyMessageBox.ShowMessageBoxInfo("调度失败！");
                        }
                    }
                    else
                    {
                        lblTips.Visible = true;
                    }
                }
                catch (Exception)
                {
                    lblTips.Visible = true;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void txtDispatchCount_TextChanged(object sender, EventArgs e)
        {
            lblTips.Visible = false;
        }
    }
}